package org.huangrui.spark.java.core.rdd.operate.action;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

import java.util.Arrays;
import java.util.List;

/**
 * @Author hr
 * @Create 2024-10-18 8:35
 */
public class Spark03_Operate_Action {
    public static void main(String[] args) {
        final SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("spark");
        final JavaSparkContext jsc = new JavaSparkContext(conf);
        final List<Integer> nums = Arrays.asList(4, 2, 3, 1);
        final JavaRDD<Integer> rdd = jsc.parallelize(nums, 2);
        final JavaRDD<Integer> newRdd = rdd.map(num -> num * 2);

        // TODO collect用于采集数据
        final List<Integer> collect = newRdd.collect();
        // TODO count获取结果数量
        long count = newRdd.count();
        // TODO first获取结果的第一个值
        Integer first = newRdd.first();
        // TODO take获取前n个结果
        List<Integer> take = newRdd.take(3);

        System.out.println("collect:" + collect);//[8, 4, 6, 2]
        System.out.println("count:" + count);//[4]
        System.out.println("first:" + first);//[8]
        System.out.println("take:" + take);  //[8, 4, 6]

        jsc.close();
    }

}
